<template>
  <div class="menuConfiguration">
    <el-card class="box-card">
      <!-- 头部 -->
      <template #header>
        <div class="card-header">
          <span>工单管理</span>
        </div>
      </template>
      <!-- 选项卡部分 -->
      <div class="tab-box">
        <div
          v-for="item in tab"
          :key="item.type"
          class="tab-item"
          :class="{ active: current === item.type }"
          @click="setActive(item.type)"
        >
          {{ item.name }}
        </div>
      </div>
      <keep-alive>
        <component :is="currentComponent" @submit="submit"></component>
      </keep-alive>
    </el-card>

    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>历史工单记录</span>
          <div class="search-form">
            <el-form :inline="true" :model="searchForm" class="searchForm">
              <el-form-item class="item" style="width: 150px">
                <el-select
                  v-model="searchForm.type"
                  :popper-options="{
                    modifiers: [
                      { name: 'computeStyles', options: { adaptive: false } },
                    ],
                  }"
                  @change="handleSearch"
                >
                  <el-option
                    v-for="item in tab"
                    :key="item.type"
                    :label="item.name"
                    :value="item.type"
                    :placeholder="item.name"
                  />
                </el-select>
              </el-form-item>
              <el-form-item class="item" style="width: 150px">
                <el-select
                  v-model="searchForm.status"
                  placeholder="处理状态"
                  @change="handleSearch"
                >
                  <el-option
                    v-for="item in status"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                    :disabled="item.disabled"
                  />
                </el-select>
              </el-form-item>

              <el-form-item class="item">
                <el-input
                  :prefix-icon="Search"
                  v-model="searchForm.keyword"
                  placeholder="工单编号/用户手机号"
                >
                  <template #append>
                    <span class="search" @click="handleSearch">搜索</span>
                  </template>
                </el-input>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </template>
      <div class="table-box">
        <el-table
          :data="tableData"
          style="width: 100%"
          height="300"
          max-height="400"
        >
          <el-table-column prop="orderNo" label="工单编号">
            <template #default="scope">
              {{ scope.row.orderNo }}
              <el-icon
                @click="copyToClipboard(scope.row.orderNo)"
                style="cursor: pointer; margin-left: 8px"
              >
                <CopyDocument />
              </el-icon>
            </template>
          </el-table-column>

          <el-table-column
            el-table-column
            prop="orderStudent"
            label="用户账号"
            width="120"
          >
            <template #default="scope">
              <!-- <span>{{  orderStudentFn(scope.row.orderStudent) }}</span> -->
              <span
                v-html="scope.row.orderStudent.replace(/\\n/g, '<br>')"
              ></span>
            </template>
          </el-table-column>

          <el-table-column prop="creationName" label="申请人" />
          <el-table-column prop="create_time" label="申请时间" />
          <el-table-column prop="type" label="工单类型">
            <template #default="scope">
              <span>{{ type[scope.row.type] }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="content" label="申请内容" />

          <!-- 申请原因列，显示图片 -->
          <el-table-column prop="remark" label="申请原因">
            <template #default="scope">
              <span
                style="cursor: pointer; color: blue"
                @click="handleImageClick(scope.row.auditImage)"
              >
                {{ scope.row.remark }}
              </span>
            </template>
          </el-table-column>

          <el-table-column prop="status" label="处理状态">
            <template #default="scope">
              <span>
                {{ status[scope.row.status].name }}
              </span>
            </template>
          </el-table-column>
          <el-table-column prop="auditRemark" label="处理回执" />
        </el-table>

        <div class="pagintion">
          <el-pagination
            :current-page="tableConfig.page"
            :page-size="tableConfig.pageSize"
            v-if="tableConfig.total"
            :page-sizes="[10, 20, 30, 40]"
            layout="total, sizes, prev, pager, next, jumper"
            :total="tableConfig.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </el-card>
    <el-image-viewer
      v-if="imageViewerVisible"
      :url-list="imageList"
      :initial-index="0"
      @close="imageViewerVisible = false"
    />
  </div>
</template>

<script setup>
import { classList } from '@/api/workOrder/index';
import { reactive, ref, onMounted, watch, computed } from 'vue';
import { ElMessage } from 'element-plus';

// // 引入组件
import KeDanTuiFei from './components/KeDanTuiFei.vue';
import HuiYuanKaTuiFei from './components/HuiYuanKaTuiFei.vue';
import XiuGaiNianFen from './components/XiuGaiNianFen.vue';
import XiuGaiQuanXian from './components/XiuGaiQuanXian.vue';
import ZhuXiaoYongHu from './components/ZhuXiaoYongHu.vue';
import XiuGaiChengJi from './components/XiuGaiChengJi.vue';
import YiJianFanKui from './components/YiJianFanKui.vue';
import { Search } from '@element-plus/icons-vue';
import { getWorkList } from '@/api/workOrder/index';
import common from '@/modules';
const { userVipSaveApi } = common;
const imageViewerVisible = ref(false);
// 选项卡数据
const tab = ref([]);
const current = ref(3);
// 组件映射
const componentMap = {
  1: KeDanTuiFei,
  2: HuiYuanKaTuiFei,
  3: XiuGaiNianFen,
  4: XiuGaiQuanXian,
  5: ZhuXiaoYongHu,
  6: XiuGaiChengJi,
  7: YiJianFanKui,
};

const status = ref([
  {
    id: 0,
    name: '待处理',
  },
  {
    id: 1,
    name: '处理中',
  },
  {
    id: 2,
    name: '已处理',
  },
  {
    id: 3,
    name: '已驳回',
  },
]);
const type = {
  1: '客单退费',
  2: '会员卡退费',
  3: '修改年份',
  4: '修改权限',
  5: '注销用户账号',
  6: '修改成绩',
  7: '意见反馈',
};
// 当前显示的组件
const currentComponent = computed(() => {
  return componentMap[current.value];
});

const tableData = ref([]);
const tableConfig = reactive({
  page: 1,
  pageSize: 10,
  total: 0,
});
const searchForm = reactive({
  status: '',
  keyword: '',
  type: current.value,
});
const setActive = (type) => {
  current.value = type;
  tableConfig.page = 1;
  searchForm.type = type;
  getTableData();
};
// 图片预览相关
const imageList = ref([]); // 用于保存所有的图片列表

// 处理点击图片事件
const handleImageClick = (image) => {
  imageViewerVisible.value = true;
  imageList.value = [image]; // 只显示单张图片
};

// 复制到剪贴板功能
const copyToClipboard = (text) => {
  navigator.clipboard
    .writeText(text)
    .then(() => {
      ElMessage.success('复制成功');
    })
    .catch(() => {
      ElMessage.error('复制失败');
    });
};
//搜索
const handleSearch = () => {
  tableConfig.page = 1;
  getTableData();
};
const getTableData = () => {
  delete tableConfig.total;
  getWorkList({ ...searchForm, ...tableConfig }).then((res) => {
    const { data } = res;
    tableData.value = data.data;
    tableConfig.total = data.total;
  });
};
watch(
  () => current.value,
  (newVal) => {
    current.value = newVal;
  },
  { deep: true }
);
// 分页
// 切换大小
const handleSizeChange = (val) => {
  tableConfig.pageSize = val;
  getTableData();
};
// 切换页码
const handleCurrentChange = (val) => {
  tableConfig.page = val;
  getTableData();
};
// 表头数据
const intiClassList = () => {
  classList().then((res) => {
    const { data } = res;
    tab.value = data;
    current.value = data[0].type;
  });
};
// 表单提交
const submit = async (formValue) => {
  try {
    const res = await userVipSaveApi({ ...formValue, type: current.value });
    if (res.code == 20000) {
      ElMessage.success(res.msg);
    }
    getTableData();
  } catch (error) {
    ElMessage.error(error.msg);
  }
};

onMounted(() => {
  intiClassList();
  getTableData();
});
</script>

<style lang="scss" scoped>
.menuConfiguration {
  .tab-box {
    display: flex;
    gap: 40px;
    margin-bottom: 20px;
    border-bottom: 1px solid #e5e6eb;

    .tab-item {
      color: #4e5969;
      font-size: 20px;
      cursor: pointer;

      &.active {
        color: #1d2129;
        font-weight: 600;
        border-bottom: 3px solid #1d2129;
        padding-bottom: 10px;
      }
    }
  }

  /* 设置一个足够高的 z-index 值 */
  .box-card {
    .card-header {
      display: flex;
      justify-content: space-between;
    }
  }

  :deep(.el-input-group__append) {
    border: 1px solid #165dff !important;
    background: #165dff !important;
    color: #fff;
    cursor: pointer;
    padding: 0 !important;
    box-shadow: none !important;

    &:hover {
      background: #4080ff !important;
    }
  }

  .submitbtn {
    width: 170px;
    height: 40px;
    color: #fff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #1c5dff;
    border-radius: 4px;
    margin-top: 40px;
    margin-left: 140px;

    &:hover {
      background: #4080ff;
    }
  }

  .pagintion {
    float: right;
    margin-top: 15px;
  }

  .item {
    margin-left: 10px;
  }
}

::v-global(.el-upload) {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 153px;
  height: 153px;

  .el-icon {
    font-size: 28px;
    color: #8c939d;
  }
}
</style>
